import request from '@/utils/request.ts';
import type {
  UserListResData,
  UserInfo,
  UserInfoResData,
  UserRolesListResData,
  AssignRolesReqData
} from './user-type.ts';
import type { ResponseData } from '../type.ts';

enum API {
  GET_USER_LIST = '/admin/acl/user',
  GET_USER_BY_ID = '/admin/acl/user/get',
  SAVE_USER = '/admin/acl/user/save',
  UPDATE_USER = '/admin/acl/user/update',
  GET_ASSIGN_ROLE = '/admin/acl/user/toAssign',
  DO_ASSIGN_ROLE = '/admin/acl/user/doAssignRole',
  REMOVE_USER = '/admin/acl/user/remove',
  REMOVE_USERS = '/admin/acl/user/batchRemove'
}

// 获取用户列表
export const reqGetUserList = (page: number, limit: number, name?: string) =>
  request.get<any, UserListResData>(
    `${API.GET_USER_LIST}/${page}/${limit}${name ? `?name=${name}` : ''}`
  );

// 根据id获取用户详情
export const reqGetUserById = (id: number) =>
  request.get<any, UserInfoResData>(`${API.GET_USER_BY_ID}/${id}`);

// 提交用户信息
export const reqSaveOrUpdateUser = (data: UserInfo) => {
  const url = data.id ? API.UPDATE_USER : API.SAVE_USER;
  const method = data.id ? 'put' : 'post';
  return request[method]<any, ResponseData>(url, data);
};

// 获取用户角色信息
export const reqGetAssignRole = (id: number) =>
  request.get<any, UserRolesListResData>(`${API.GET_ASSIGN_ROLE}/${id}`);

// 设置用户角色信息
export const reqDoAssignRole = (data: AssignRolesReqData) =>
  request.post<any, ResponseData>(API.DO_ASSIGN_ROLE, data);

// 删除用户
export const reqRemoveUser = (id: number) =>
  request.delete<any, ResponseData>(`${API.REMOVE_USER}/${id}`);

// 批量删除用户
export const reqRemoveUsers = (idList: number[]) =>
  request.delete<any, ResponseData>(API.REMOVE_USERS, { data: idList });
